package com.example.demo.excel.valid;

/**
 * @author yg.zhao
 * @description 导入校验
 * @date 2022/12/12
*/

import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.example.demo.excel.valid.ExcelValid.*;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @author yg.zhao
 * @description excel导入校验
 * @date 2022/12/12
*/
public class ExcelImportValid {

    /**
     * Excel导入字段校验
     *
     * @param object 校验的JavaBean 其属性须有自定义注解
     */
    @SneakyThrows
    public static String valid(Object object) throws ExcelValidException {
        StringBuilder sbf = new StringBuilder();
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            //设置可访问
            field.setAccessible(true);
            //属性的值
            Object fieldValue = null;
            try {
                fieldValue = field.get(object);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }

            //是否包含必填校验注解
            boolean isExcelValid = field.isAnnotationPresent(ExcelMustValid.class);
            if (isExcelValid && Objects.isNull(fieldValue)) {
                ExcelMustValid annotation = field.getAnnotation(ExcelMustValid.class);
                sbf.append("【").append(annotation.name()).append("】有未填值项");
            }

            //是否包含字符串长度校验注解
            boolean isExcelStrValid = field.isAnnotationPresent(ExcelStrValid.class);
            if (isExcelStrValid) {
                ExcelStrValid annotation = field.getAnnotation(ExcelStrValid.class);
                if (ObjectUtils.isNotEmpty(fieldValue)) {
                    String cellStr = fieldValue.toString();
                    if (cellStr.length() > annotation.length()) {
                        sbf.append("【").append(annotation.name()).append("】长度有误");
                    }
                }
            }

            //是否包含int类型校验注解
            boolean isExcelIntValid = field.isAnnotationPresent(ExcelIntValid.class);
            if (isExcelIntValid) {
                if (fieldValue instanceof Integer) {
                    int cellInt = Integer.parseInt(fieldValue.toString());
                    ExcelIntValid annotation = field.getAnnotation(ExcelIntValid.class);
                    if (cellInt < annotation.min() || cellInt > annotation.max()) {
                        sbf.append("【").append(annotation.name()).append("】数值范围有误");
                    }
                }
            }

            //是否包含decimal类型注解
            boolean isExcelDecimalValid = field.isAnnotationPresent(ExcelDecimalValid.class);
            if (isExcelDecimalValid) {
                BigDecimal cellDecimal;
                ExcelDecimalValid annotation = field.getAnnotation(ExcelDecimalValid.class);
                try {
                    cellDecimal = new BigDecimal(field.toString());
                    if (cellDecimal.compareTo(new BigDecimal(annotation.min())) < 0 || cellDecimal.compareTo(new BigDecimal(annotation.max())) > 0) {
                        sbf.append("【").append(annotation.name()).append("】数值范围有误");
                    }
                } catch (Exception e) {
                    sbf.append("【").append(annotation.name()).append("】不是小数类型");
                }
            }

            // 是否包含正则校验
            boolean isExcelPatternValid = field.isAnnotationPresent(ExcelPatternValid.class);
            if (isExcelPatternValid) {
                String cellStr = fieldValue.toString();
                ExcelPatternValid annotation = field.getAnnotation(ExcelPatternValid.class);
                if (StringUtils.isBlank(annotation.name())) {
                    sbf.append("【").append(annotation.name()).append("】有未填值");
                }
                if (StringUtils.isNotBlank(cellStr) && !Pattern.matches(annotation.regexp(), cellStr)) {
                    sbf.append("【").append(annotation.name()).append("】不符合规则");
                }
            }
            if (sbf.length() > 0) {
                sbf.append(",");
            }
        }
        if (sbf.length() > 0) {
            return sbf.substring(0, sbf.length() - 1);
        }
        return null;
    }
}

